<?php namespace App\Http\Controllers;

use App\CoinLog;
use App\Goods;
use App\GoodsAttr;
use App\Orders;
use App\Signed;
use App\SysBanner;
use App\User;
use App\UserAddress;
use App\UserInfo;
use EasyWeChat\Payment\Order;
use Illuminate\Contracts\Auth\Guard;
use Illuminate\Http\Request;
use Session;

class HomeController extends Controller
{
    public function login(){
        return view('login');
    }

    public function register(){
        return view('register');
    }

    public function baibaoxiang(Guard $guard){
        if($guard->guest())
            return redirect(route('Api.Auth.wechatLogin'));

        /** @var User $user */
        $user = $guard->user();

        $userinfo = null;
        if($user){
            $userinfo = $user->userinfo;
        }

        $bannerList = SysBanner::whereStatus(1)->orderBy('sort','asc')->orderBy('created_at','desc')->get();

        return view('baibaoxiang',[
            'user'     => $user,
            'userinfo' => $userinfo,
            'banners'  => $bannerList
        ]);
    }

    public function userinfo(Guard $guard){

        if($guard->guest())
            return redirect(route('Home.Auth.login'));

        /** @var User $user */
        $user = $guard->user();

        $userinfo = $user->userinfo;
        return view('userinfo',['user'=>$user,'userinfo'=>$userinfo]);
    }

    public function myexchange(Guard $guard,Request $request){
        $type = !is_null($request->get('type')) ? $request->get('type') : 'dfh';
        if($guard->guest())
            return redirect(route('Home.Auth.login'));
        /** @var User $user */
        $user = $guard->user();
        $myOrders = null;

        switch ($type){
            case 'dfh':
                $myOrders = Orders::whereUserId($user->id)->whereIsDelivery(0)->whereStatus(1)->orderBy('created_at','desc')->get();
                break;
            case 'yfh':
                $myOrders = Orders::whereUserId($user->id)->whereIsDelivery(1)->whereStatus(1)->orderBy('created_at','desc')->get();
                break;
            case 'ywc':
                $myOrders = Orders::whereUserId($user->id)->whereIsDelivery(1)->whereStatus(2)->orderBy('created_at','desc')->get();
                break;
        }

        foreach ($myOrders as $order){
            /** @var Orders $order */
            $thatGood = Goods::whereId($order->good_id)->first();
            if($thatGood){
                $order->good = $thatGood;
            }else{
                $order->good = null;
            }
        }

        return view('myexchange',['my_orders'=>$myOrders,'type'=>$type]);
    }

    public function goodsList(Guard $guard,Request $request){
        $model = !is_null($request->get('model')) ? $request->get('model') : 0;

        $allGoods = '';
        if($model == 0){
            $allGoods = Goods::whereIsshop(1)->whereType(1)->orderBy('created_at','desc')->get();
        }elseif($model == 100){
            $allGoods = Goods::whereIsshop(1)->whereType(1)->whereCoin(100)->orderBy('created_at','desc')->get();
        }elseif($model == 50){
            $allGoods = Goods::whereIsshop(1)->whereType(1)->whereCoin(50)->orderBy('created_at','desc')->get();
        }

        if($guard->guest())
            return redirect(route('Home.Auth.login'));
        /** @var User $user */
        $user = $guard->user();

        /** @var UserInfo $userInfo */
        $userInfo = UserInfo::whereUserId($user->id)->first();

        return view('goods-list',['all_goods'=>$allGoods,'userinfo'=>$userInfo,'model'=>$model]);
    }

    public function goodsListCash(Guard $guard,Request $request){
        $model = !is_null($request->get('model')) ? $request->get('model') : 0;

        $allGoods = Goods::whereIsshop(1)->whereType(2)->orderBy('created_at','desc')->get();

        if($guard->guest())
            return redirect(route('Home.Auth.login'));
        /** @var User $user */
        $user = $guard->user();

        /** @var UserInfo $userInfo */
        $userInfo = UserInfo::whereUserId($user->id)->first();

        return view('goods-list-cash',['all_goods'=>$allGoods,'userinfo'=>$userInfo,'model'=>$model]);
    }

    public function goodsDetail(Guard $guard,Request $request){
        $id = $request->get('id');

        if($guard->guest())
            return redirect(route('Home.Auth.login'));
        /** @var User $user */
        $user = $guard->user();

        $thatGoods = Goods::whereId($id)->whereIsshop(1)->first();

        return view('goods-detail',['good'=>$thatGoods]);
    }

    public function goodsDetailCash(Guard $guard,Request $request){
        $id = $request->get('id');

        if($guard->guest())
            return redirect(route('Home.Auth.login'));
        /** @var User $user */
        $user = $guard->user();

        $thatGoods = Goods::whereId($id)->whereIsshop(1)->first();

        return view('goods-detail-cash',['good'=>$thatGoods]);
    }

    public function goodsBuy(Guard $guard,Request $request){
        $id        = $request->get('id');
        $addressId = !is_null($request->get('address_id')) ? $request->get('address_id') : 0;

        if($guard->guest())
            return redirect(route('Home.Auth.login'));
        /** @var User $user */
        $user = $guard->user();

        $thatGoods = Goods::whereId($id)->whereIsshop(1)->first();

        if($thatGoods){
            $goodAttrs = GoodsAttr::whereGoodId($thatGoods->id)->get();

            $attrArrs = array();
            for($i=0;$i<ceil(count($goodAttrs)/2);$i++)
            {
                $attrArrs[] = array_slice(json_decode($goodAttrs,true), $i * 2 ,2);
            }

            $thatGoods->attrs = $attrArrs;
        }

        /** @var UserInfo $userInfo */
        $userInfo = UserInfo::whereUserId($user->id)->first();

        $thatAddress = null;
        if($addressId == 0){
            $thatAddress = UserAddress::whereUserId($user->id)->whereIsDefault(1)->first();
        }else{
            $thatAddress = UserAddress::whereId($addressId)->first();
        }

        return view('goods-buy',['good'=>$thatGoods,'userinfo'=>$userInfo,'thatAddress'=>$thatAddress]);
    }

    public function goodsBuyCash(Guard $guard,Request $request){
        $id        = $request->get('id');
        $addressId = !is_null($request->get('address_id')) ? $request->get('address_id') : 0;

        if($guard->guest())
            return redirect(route('Home.Auth.login'));
        /** @var User $user */
        $user = $guard->user();

        $thatGoods = Goods::whereId($id)->whereIsshop(1)->first();

        if($thatGoods){
            $goodAttrs = GoodsAttr::whereGoodId($thatGoods->id)->get();

            $attrArrs = array();
            for($i=0;$i<ceil(count($goodAttrs)/2);$i++)
            {
                $attrArrs[] = array_slice(json_decode($goodAttrs,true), $i * 2 ,2);
            }

            $thatGoods->attrs = $attrArrs;
        }

        /** @var UserInfo $userInfo */
        $userInfo = UserInfo::whereUserId($user->id)->first();

        $thatAddress = null;
        if($addressId == 0){
            $thatAddress = UserAddress::whereUserId($user->id)->whereIsDefault(1)->first();
        }else{
            $thatAddress = UserAddress::whereId($addressId)->first();
        }

        return view('goods-buy-cash',['good'=>$thatGoods,'userinfo'=>$userInfo,'thatAddress'=>$thatAddress]);
    }

    public function confirmOrder(Guard $guard,Request $request){
        $orderId = $request->get('order_id');

        if($guard->guest())
            return redirect(route('Home.Auth.login'));
        /** @var User $user */
        $user = $guard->user();

        /** @var Orders $thatOrder */
        $thatOrder = Orders::whereId($orderId)->first();

        if($thatOrder){
            /** @var Goods $thatGood */
            $thatGood = Goods::whereId($thatOrder->good_id)->first();

            $thatOrder->good = $thatGood;
        }

        return view('confirm-order',['order'=>$thatOrder]);
    }

    public function confirmOrderCash(Guard $guard,Request $request){
        $orderId = $request->get('order_id');

        if($guard->guest())
            return redirect(route('Home.Auth.login'));
        /** @var User $user */
        $user = $guard->user();

        /** @var Orders $thatOrder */
        $thatOrder = Orders::whereId($orderId)->first();

        if($thatOrder){
            /** @var Goods $thatGood */
            $thatGood = Goods::whereId($thatOrder->good_id)->first();

            $thatOrder->good = $thatGood;
        }

        return view('confirm-order-cash',['order'=>$thatOrder]);
    }

    public function changeInfo(Guard $guard,Request $request){
        $type = $request->get('type');

        if($guard->guest())
            return redirect(route('Home.Auth.login'));
        /** @var User $user */
        $user = $guard->user();

        $content = '';

        /** @var UserInfo $userInfo */
        $userInfo = UserInfo::whereUserId($user->id)->first();
        $title = '';

        if($userInfo){
            switch ($type){
                case 'nickname':
                    $content = $userInfo->nickname;
                    $title = '修改昵称';
                    break;
                case 'realname':
                    $content = $userInfo->realname;
                    $title = '修改姓名';
                    break;
                case 'qq':
                    $content = $userInfo->qq;
                    $title = '修改QQ';
                    break;
                case 'wechat':
                    $content = $userInfo->wechat;
                    $title = '修改微信号';
                    break;
            }
        }

        return view('change-info',['type'=>$type,'content'=>$content,'title'=>$title]);
    }

    public function myAddress(Guard $guard,Request $request){
        $type = !is_null($request->get('type')) ? $request->get('type') : 'change';
        if($guard->guest())
            return redirect(route('Home.Auth.login'));

        /** @var User $user */
        $user = $guard->user();

        $addressList = UserAddress::whereUserId($user->id)->orderBy('created_at','desc')->get();

        return view('my-address',['address_list'=>$addressList,'type'=>$type]);
    }

    public function changeAddress(Guard $guard,Request $request){
        $id   = $request->get('id');

        if($guard->guest())
            return redirect(route('Home.Auth.login'));
        /** @var User $user */
        $user = $guard->user();

        $id = !is_null($request->get('id')) ? $request->get('id') : 0;

        $thatAddress = UserAddress::whereId($id)->whereUserId($user->id)->first();
        return view('change-address',['id'=>$id,'address'=>$thatAddress]);
    }

    public function forgetPassword(){
        return view('forget-password');
    }

    public function changePassword(){
        return view('change-password');
    }

    public function myNewAddress(Guard $guard,Request $request){
        $type = !is_null($request->get('type')) ? $request->get('type') : 'change';
        $gid  = !is_null($request->get('gid')) ? $request->get('gid') : 0;
        $num  = !is_null($request->get('num')) ? $request->get('num') : 1;

        if($guard->guest())
            return redirect(route('Home.Auth.login'));

        /** @var User $user */
        $user = $guard->user();

        $addressList = UserAddress::whereUserId($user->id)->orderBy('created_at','desc')->get();


        return view('my-new-address',['address_list'=>$addressList,'type'=>$type,'gid'=>$gid,"num"=>$num]);
    }

    public function myShare(Guard $guard,Request $request){
        if($guard->guest())
            return redirect(route('Home.Auth.login'));

        /** @var User $user */
        $user = $guard->user();

        $userinfo = null;
        if($user){
            $userinfo = $user->userinfo;
        }

        return view('my-share',['user'=>$user,'userinfo'=>$userinfo]);
    }

    public function qiandao(Guard $guard,Request $request){
        if($guard->guest())
            return redirect(route('Home.Auth.login'));
        /** @var User $user */
        $user = $guard->user();

        $mySignedCount = Signed::whereUserId($user->id)->count();

        $todayTime = strtotime(date('Y-m-d',time()));

        $todaySigned = Signed::whereUserId($user->id)->whereSignedTime($todayTime)->first();

        $userinfo = UserInfo::whereUserId($user->id)->first();

        return view('qiandao',['sign_count'=>$mySignedCount,'today_signed'=>$todaySigned,'userinfo'=>$userinfo]);
    }

    public function youlai(){
        return view('youlai');
    }

    public function dingzhi(){
        return view('dingzhi');
    }

    public function hujiao(){
        return view('hujiao');
    }

    public function coinDetail(Guard $guard,Request $request){
        $date = $request->get('date');

        if($guard->guest())
            return redirect(route('Home.Auth.login'));
        /** @var User $user */
        $user = $guard->user();

        $userInfo = UserInfo::whereUserId($user->id)->first();

        $thatDateStart = $date.' 00:00:00';
        $thatDateEnd   = $date.' 23:59:59';
        $myCoin = null;
        if(!is_null($date) && $date != ''){
            $myCoin = CoinLog::whereUserId($user->id)->whereBetween('created_at',[$thatDateStart,$thatDateEnd])->orderBy('created_at','desc')->get();
        }else{
            $myCoin = CoinLog::whereUserId($user->id)->orderBy('created_at','desc')->get();
        }


        return view('coin-detail',['coins'=>$myCoin,'userinfo'=>$userInfo]);
    }

    public function pcIndex(){
        return view('pc-index');
    }

    public function newGoodsDetail(Guard $guard,Request $request){
        $id = $request->get('id');

        if($guard->guest())
            return redirect(route('Api.Auth.wechatLogin'));
        /** @var User $user */
        $user = $guard->user();

        $thatGoods = Goods::whereId($id)->whereIsshop(1)->first();

        return view("shop.goods-detail",["good"=>$thatGoods]);
    }

    public function newOrder(Guard $guard,Request $request){
        $id        = $request->get('id');
        $num       = $request->get('num');
        $addressId = !is_null($request->get('address_id')) ? $request->get('address_id') : 0;

        if($guard->guest())
            return redirect(route('Home.Auth.login'));
        /** @var User $user */
        $user = $guard->user();

        $thatGoods = Goods::whereId($id)->whereIsshop(1)->first();

        if($thatGoods){
            $goodAttrs = GoodsAttr::whereGoodId($thatGoods->id)->get();

            $attrArrs = array();
            for($i=0;$i<ceil(count($goodAttrs)/2);$i++)
            {
                $attrArrs[] = array_slice(json_decode($goodAttrs,true), $i * 2 ,2);
            }

            $thatGoods->attrs = $attrArrs;
        }

        if($num > $thatGoods->stock){
            $num = $thatGoods->stock;
        }

        $eAmount = 8;
        if($num >=5){
            $eAmount = 0;
        }

        $amount = $thatGoods->amount * $num + $eAmount;

        /** @var UserInfo $userInfo */
        $userInfo = UserInfo::whereUserId($user->id)->first();

        $thatAddress = null;
        if($addressId == 0){
            $thatAddress = UserAddress::whereUserId($user->id)->whereIsDefault(1)->first();
        }else{
            $thatAddress = UserAddress::whereId($addressId)->first();
        }

        return view('shop.order',[
            'good'=>$thatGoods,
            'userinfo'=>$userInfo,
            'thatAddress'=>$thatAddress,
            "num"=>$num,
            "e_amount"=>$eAmount,
            "amount"=>$amount
        ]);
    }

    public function showGoods(){
        return view('shop.show-goods');
    }
}